package com.hula.onion.book.server.spider.platforms.yigangjinrong;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/1/27.
 */
@Component
public class YiGangJRSpiderHelper {

    private static final Logger logger = LoggerFactory.getLogger(YiGangJRSpiderHelper.class);

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("username", form.getUserName());
        map.put("password",form.getPassword());
        map.put("validCode", "");
        map.put("tokenId", "");
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        try {
            Document dc = Jsoup.parse(html);
            Element el = dc.selectFirst("div.money-div");
            return new CommonResponse(el.getAllElements().get(2).text());
        } catch (Exception e) {
            logger.error("[YiGangJRSpiderHelper] parseAccountBalance fail",e);
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
